<?php
/**
 * 系统设置
 */

namespace App\OpenApi\AdminApi\Sys;

use App\Model\Core\Sys\SysConfigModel;
use App\Service\OpenApi\AdminApi\OpenBaseApi;
use App\Service\ParamsService;

class SysConfigApi extends OpenBaseApi
{
    /**
     * 设置系统配置
     * @return array|bool|int
     */
    public function store()
    {
        $config_item = trim($this->input('config_item'));
        $config_val = trim($this->input('config_val'));

        if (empty($config_item)) {
            return alert_info(1, '未设置配置键');
        }
        if (is_null($config_val) || $config_val == '') {
            return alert_info(1, '未设置配置值');
        }
        if ($config_item == ParamsService::HEHUAN_PATIENT_POP) {
            if (!array_key_exists($config_val, ParamsService::$hehuan_pop_list)) {
                return alert_info(1, '免责提示错误');
            }
        }
        if (ParamsService::$config_item_list[$config_item]['type'] == 'fulltext') {
            $config_val = easy_convert_html_to_store($config_val, false);
        }
        $SysConfigModel = SysConfigModel::getInstance();
        $res = $SysConfigModel->setConfig($config_item, $config_val, $this->operate_by);
        if (!$res) {
            return alert_info(1, '操作失败');
        }
        return alert_info(0, '操作成功');
    }

    /**
     * 获取配置项信息
     * @return array
     */
    public function info()
    {
        $config_item = trim($this->input('config_item'));

        if (empty($config_item)) {
            return alert_info(1, '未设置配置键');
        }
        $SysConfigModel = SysConfigModel::getInstance();
        $config_info = $SysConfigModel->getConfig($config_item);
        if (!empty($config_info) && ParamsService::$config_item_list[$config_item]['type'] == 'fulltext') {
            if (!empty($config_info['config_val'])) {
                $config_info['config_val'] = convert_store_to_html($config_info['config_val']);
            }
        }
        return alert_info(0, 'ok', $config_info);
    }
}
